package cn.edu.sctu.sctu_lysd.view.act;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import java.util.List;

import cn.edu.sctu.sctu_lysd.R;
import cn.edu.sctu.sctu_lysd.bean.FolderBean;
import cn.edu.sctu.sctu_lysd.utils.ImageLoader;


/**
 * Created by 鑫爷您慢点 on 2016/8/3.
 */
public class ListImageDirPopupWindow extends PopupWindow {
    private int mWidth;
    private int mHeight;
    private View mConvertView;
    private ListView mListView;



    private List<FolderBean> mDatas;

    private OnDirSelectedListener mOnDirSelectedListener;

    public void setOnDirSelectedListener(OnDirSelectedListener mOnDirSelectedListener) {
        this.mOnDirSelectedListener = mOnDirSelectedListener;
    }

    public ListImageDirPopupWindow(Context context, List<FolderBean> mDatas) {
        this.mDatas = mDatas;
        calWidthAndHeight(context);
        mConvertView = LayoutInflater.from(context).inflate(R.layout.photo_popup_main, null);

        setContentView(mConvertView);
        setWidth(mWidth);
        setHeight(mHeight);

        setFocusable(true);//设置可触摸
        setTouchable(true);//
        setOutsideTouchable(true);//设置外部可触摸
        setBackgroundDrawable(new BitmapDrawable());//点击外面消失

        setTouchInterceptor(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {//判断动作是点击在外部
                    dismiss();//消失
                    return true;
                }
                return false;
            }
        });
        initView(context);
        initEvent();
    }

    private void initView(Context context) {
        mListView = (ListView) mConvertView.findViewById(R.id.id_list_dir);

        mListView.setAdapter(new ListDirAdapter(context, mDatas));
    }

    private void initEvent() {
            mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    if (mOnDirSelectedListener != null){
                        mOnDirSelectedListener.onSelected(mDatas.get(position));
                    }
                }
            });
    }

    /**
     * 计算popupWindow的高和宽
     *
     * @param context
     */
    private void calWidthAndHeight(Context context) {

        /**
         * 获取屏幕高和宽
         */
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics outMetrics = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(outMetrics);

        //
        mWidth = outMetrics.widthPixels;
        mHeight = (int) (outMetrics.heightPixels * 0.7);

    }

    private class ListDirAdapter extends ArrayAdapter<FolderBean> {
        private LayoutInflater mInflater;
        private List<FolderBean> mDatas;

        public ListDirAdapter(Context context, List<FolderBean> objects) {
            super(context, 0, objects);
            mInflater = LayoutInflater.from(context);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder = null;
            if (convertView == null){
                holder = new ViewHolder();
                convertView = mInflater.inflate(R.layout.photo_item_popup_main,parent,false);

                holder.mImg = (ImageView) convertView.findViewById(R.id.id_id_dir_item_image);
                holder.mDirName = (TextView) convertView.findViewById(R.id.id_id_dir_item_name);
                holder.mDirCount = (TextView) convertView.findViewById(R.id.id_id_dir_item_count);

                convertView.setTag(holder);
            }else {
                holder = (ViewHolder) convertView.getTag();
            }

            FolderBean bean = getItem(position);

            //重置
            holder.mImg.setImageResource(R.mipmap.photo_pictures_no);
            //设置ImageView图片
            ImageLoader.getInstance(2, ImageLoader.Type.LIFO).loadImage(bean.getFirstImgPath(), holder.mImg,false);

            holder.mDirCount.setText(bean.getCount() + "张");
            holder.mDirName.setText(bean.getName());

            return convertView;

        }


        private class ViewHolder{
            ImageView mImg;
            TextView mDirName;
            TextView mDirCount;
        }
    }
    public interface OnDirSelectedListener{
        void onSelected(FolderBean folderBean);
    }
}
